var $table = $("#orderTab");

$(function () {
	//时间控件
	$.initDateScope('startTime','endTime',false);
    initTable();
    //获取公司名称
    var comnum = $("#comnumId").val();
    //分公司信息的下拉列表
   	$.ajax({
           url: "../company/getBranchCompanyIdAndName.htm",
           cache: false,
           type: "get",
           dataType: "json",
           success: function (result) {
               if (result.status == 0) {
               	var data = result.data;
               	addOptions(data, "#comnumId");
                   return;
               }
           }
    });

    //条件查询
    $("#queryOrderBtn").click(function () {
        $table.bootstrapTable('refresh');	 //使用post提交
    });
   
});

function initTable() {
    $table.bootstrapTable({
        dataType: "json",
        showColumns: true, 
        pagination: true, //分页
        striped: true,
        cache: false,	//是否使用缓存，默认是true
        pageSize: 10,
        pageNumber: 1,
        queryParamsType: 'limit',
        smartDisplay: true,
        sidePagination: "server",
        method: "post",	//使用post提交
        queryParams: queryParams,
        url: '../order/getBranchCompanyPaySucOrderPage.htm',
        contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
        columns: [
            {
                title: "序号",
                align: 'center',
                formatter:function(value,row,index){
                    return index + 1;
                }
            },
            {
                title: "订单号",
                field: "orderNum",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "寄件人姓名",
                field: "senderName",
                align: 'center', 
                valign: 'middle',
                formatter: function (value, row, index) {
                   if(row.senderName != null){
                       return '<a type="button" onclick="orderListEdit(\'' + row.senderId + '\',\'' + 1 + '\',\'' + row.orderNum + '\')">'+row.senderName+'</a>';
                   }
                   return '-';
                }
            },
            {
                title: "寄件人电话",
                field: "senderPhone",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "寄件人地址",
                field: "senderAddr",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "收件人姓名",
                field: "reciverName",
                align: 'center',
                valign: 'middle',
                formatter: function (value, row, index) {
                   if(row.reciverName != null){
                	   return '<a type="button" onclick="orderListEdit(\'' + row.reciverId + '\',\'' + 2 + '\',\'' + row.orderNum + '\')">'+row.reciverName+'</a>';
                   }
                   return '-';
                }
            },
            {
                title: "收件人电话",
                field: "reciverPhone",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "收件人地址",
                field: "reciverAddr",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "货物名称",
                field: "cargoName",
                align: 'center',
                valign: 'middle',
                formatter: function (value, row, index) {
                   if(row.cargoName != null){
                	   return '<a type="button" onclick="orderListEdit(\'' + row.cargoId + '\',\'' + 3 + '\',\'' + row.orderNum + '\')">'+row.cargoName+'</a>';
                   }
                   return '-';
                }
            },
            {
                title: "件数",
                field: "cargoAmount",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "仓库名",
                field: "storageName",
                align: 'center',
                valign: 'middle',
                visible:false,
                formatter: function (value, row, index) {
                   if(row.storageName != null){
                       return '<a type="button" onclick="orderListEdit(\'' + row.storageId + '\',\'' + 4 + '\',\'' + row.orderNum + '\')">'+row.storageName+'</a>';
                   }
                   return '-';
                }
            },
            /*{
                title: "仓储员",
                field: "stSaName",
                align: 'center',
                valign: 'middle'
            },*/
            {
                title: "车辆车牌号",
                field: "truckCarNum",
                align: 'center',
                valign: 'middle',
                formatter: function (value, row, index) {
                   if(row.truckCarNum != null){
                       return '<a type="button" onclick="orderListEdit(\'' + row.truckId + '\',\'' + 5 + '\',\'' + row.orderNum + '\')">'+row.truckCarNum+'</a>';
                   }
                   return '-';
                }
            },
            {
                title: "司机姓名",
                field: "driverName",
                align: 'center',
                valign: 'middle',
                visible:false,
                formatter: function (value, row, index) {
                   if(row.driverName != null){
                       return '<a type="button" onclick="orderListEdit(\'' + row.driverId + '\',\'' + 6 + '\',\'' + row.orderNum + '\'">'+row.driverName+'</a>';
                   }
                   return '-';
                }
            },
            /*{
                title: "司机电话",
                field: "driverPhone",
                align: 'center',
                valign: 'middle'
            },*/
            {
                title: "支付状态",
                field: "orderManifestState",
                align: 'center',
                formatter: function(value, row, index){
                    var orderManifestState = "-";
                    if(row.orderManifestState == 0){
                        orderManifestState = '<span style="color:#ed5565;">未支付</span>';
                    }else if(row.orderManifestState == 1){
                        orderManifestState ='<span style="color:#23c6c8;">已支付</span>';
                    }else if(row.orderManifestState == 3){
                        orderManifestState ='<span style="color:#f8ac59;">支付异常</span>';
                    }
                    return orderManifestState;
                }
            },
            {
            	title: "支付金额(元)",
                field: "carriage",
                align: 'center',
                valign: 'middle'
            },
            {
            	title: "支付方式",
                field: "orderPayWay",
                align: 'center',
                formatter: function(value, row, index){
                    var pay_way = "-";
                    if(row.orderPayWay == 0){
                    	pay_way = '<span style="color:#0000FF;">微信</span>';
                    }else if(row.orderPayWay == 1){
                    	pay_way ='<span style="color:#8B008B;">支付宝</span>';
                    }else if(row.orderPayWay == 2){
                    	pay_way ='<span style="color:#483D8B;">平安支付</span>';
                    }else if(row.orderPayWay == 3){
                    	pay_way ='<span style="color:#00008B;">银行卡</span>';
                    }
                    return pay_way;
                }
            },
            {
                title: "回单状态",
                field: "orderReturnState",
                align: 'center',
                formatter: function(value, row, index){
                    var orderReturnState = "-";
                    if(row.orderReturnState == 0){
                        orderReturnState = '<span style="color:#ed5565;">未回单</span>';
                    }else if(row.orderReturnState == 1){
                        orderReturnState ='<span style="color:#23c6c8;">已回单</span>';
                    }else if(row.orderReturnState == -1){
                        orderReturnState ='<span style="color:#f8ac59;">回单异常</span>';
                    }
                    return orderReturnState;
                }
            },
            {
                title: "是否实名制",
                field: "senderIdCard",
                align: 'center',
                formatter: function(value, row, index){
            		var idCard = row.senderIdCard;
            		if(idCard && typeof(idCard) != 'undefined'){
            			idCard = '<span style="color:#ed5565;">是</span>';
            		}else{
            			idCard ='<span style="color:#23c6c8;">否</span>';
            		}
            		return idCard;
            	}
            },
            {
            	title: "货到付款",
                field: "orderFreightForward",
                align: 'center',
                formatter: function(value, row, index){
                	var of = "-";
                    if(row.orderFreightForward == 0){
                    	of = '<span style="color:#ed5565;">否</span>';
                    }else if(row.orderFreightForward == 1){
                    	of ='<span style="color:#23c6c8;">是</span>';
                    }
                    return of;
            	}
            },
            {
            	title: "运费(元)",
                field: "carriage",
                align: 'center',
                valign: 'middle'
            },
            {
            	title: "代收金额(元)",
                field: "orderCod",
                align: 'center',
                valign: 'middle'
            },
            {
            	title: "附加费用(元)",
                field: "orderAddedFee",
                align: 'center',
                valign: 'middle'
            },
            {
            	title: "备注",
                field: "orderNotes",
                align: 'center',
                valign: 'middle'
            },
            {
            	title: "插入时间",
                field: "orderInsTime",
                align: 'center',
                valign: 'middle'
            },
	        {
	            title: "公司名称",
	            field: "comnumName",
	            align: 'center',
	            valign: 'middle',
	            visible:false,
	             formatter: function (value, row, index) {
	               if(row.comnumName != null){
	                   return '<a type="button" onclick="orderListEdit(\'' + row.comnumId + '\',\'' + 7 + '\',\'' + row.orderNum + '\')">'+row.comnumName+'</a> ';
	               }
	               return '-';
	            }
	        },
            
        ],
        onClickRow: function(row, $element, field){	//当点击某一行时，该行背景颜色改变
        	var trObj = $($element[0]);
        	var dataIndex = trObj.attr('data-index');
        	$("tr[data-index != '" + dataIndex + "']").removeAttr('style');
        	trObj.attr('style', 'background: #B4EEB4');
        }
    });
}


//分页参数，条件查询时的参数
function queryParams(queryParamsType){
	var temp = {
		limit: queryParamsType.limit,   //页面大小
		offset: queryParamsType.offset,  //页码
		orderNum: $.trim($('#orderNum').val()),
		senderName: $.trim($('#senderName').val()),
		reciverName: $.trim($('#reciverName').val()),
		truckCarNum: $.trim($('#truckCarNum').val()),
		driverName: $.trim($('#driverName').val()),
		orderPayWay: $.trim($("#orderPayWay").val()),
		orderReturnState: $.trim($('#orderReturnState').val()),
		startTime: $.trim($('#startTime').val()),
		endTime: $.trim($('#endTime').val()),
		comnumId: $.trim($('#comnumId').val())
	};
	return temp;
}


//订单详情
function orderListEdit(id, type, orderNum){
	var title = '';
	switch (type) {
	case '1':
		title = '寄件人详情'
		break;
	case '2':
		title = '收件人详情'
		break;
	case '3':
		title = '货物详情'
		break;
	case '4':
		title = '仓库详情'
		break;
	case '5':
		title = '车辆详情'
		break;
	case '6':
		title = '司机详情'
		break;
	case '7':
		title = '公司详情'
		break;
	default:
		break;
	}
    layer.open({
        type: 2,
        title: '订单号【' + orderNum + '】—' + title,
        maxmin: true,
        shadeClose: true, //点击遮罩关闭层
        area : ['500px' , '750px'],
        content: '../order/suc_details_order.htm?id=' + id + '&type=' + type,
        success: function(layero, index){
        }
    });
}

//comnum的下拉列表  追加到option中
function addOptions(data, selectId){
	for(var i=0; i<data.length; i++){
		$(selectId).append('<option value="' + data[i].comnumId + '">' + data[i].comnumName + '</option>');
	}
}
